/*
 * @features: 功能
 * @description: 说明
 * @Date: 2021-09-28 23:44:52
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-10-05 15:41:54
 * @LastEditors: judu233
 */
import c3d from "../../cc";
import ExtendsLoad from "../CCExtends";

declare module "cc" {
    export interface UITransform {
        /**初始化尺寸 */
        resize(): void;
    }
}
class UITransfromExtends {
    resize() {
        let dr = c3d.view.getDesignResolutionSize();
        var s = c3d.view.getFrameSize();
        var rw = s.width;
        var rh = s.height;
        var finalW = rw;
        var finalH = rh;

        if ((rw / rh) > (dr.width / dr.height)) {
            // 如果更长，则用定高
            finalH = dr.height;
            finalW = finalH * rw / rh;
        }
        else {
            // 如果更短，则用定宽
            finalW = dr.width;
            finalH = finalW * rh / rw;
        }

        // 手工修改canvas和设计分辨率，这样反复调用也能生效。
        c3d.view.setDesignResolutionSize(finalW, finalH, c3d.ResolutionPolicy.UNKNOWN);

        let transform = this as unknown as c3d.UITransform;
        transform.width = finalW;
        transform.height = finalH;

        c3d.log(dr, "设计尺寸");
        c3d.log(s, "屏幕尺寸");
    }
}

ExtendsLoad.load(c3d.UITransform.prototype, UITransfromExtends, true);